home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / t_rex10 / regexdll.c next >
C/C++ Source or Header  |  1996-09-15  |  4KB  |  163 lines

  1. /************************************************\
  2. *                                                *
  3. *   REGEXP.DDL Interface module                  *
  4. *   Copyright (c) 1992 by Borland International  *
  5. *                                                *
  6. \************************************************/
  7.  
  8. /*  This module defines the exported interface functions
  9.  *  provided by REGEXP.DLL.  It provides the multi client
  10.  *  support managment by managing regular expression
  11.  *  handles.
  12.  */
  13.  
  14. #define  STRICT
  15.  
  16. #include <sys/types.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <limits.h>
  22.  
  23. #include <winapi.h>
  24.  
  25. #include "regexp2.h"
  26. #include "_regexp.h"
  27.  
  28. HINSTANCE Instance;    // Instance handle
  29.  
  30. // Handle managment ------------------------------------------------
  31.  
  32. #define MAX_HANDLES 128
  33.  
  34. int FreeList = 1;
  35. regexp *Handles[MAX_HANDLES];
  36.  
  37. void InitializeHandles(void)
  38. {
  39.     int i;
  40.  
  41.     for (i = 0; i < MAX_HANDLES - 1; i++)
  42.     {
  43.         Handles[i] = (regexp *)(i + 1);
  44.     }
  45.     Handles[i] = NULL;
  46. }
  47.  
  48. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  49. BOOL VerifyHandle(HREGEXP reg)
  50. {
  51.     return reg && (int)reg < MAX_HANDLES &&
  52.        Handles[(int) reg] != NULL  &&
  53.        UCHARAT(Handles[(int) reg]->program) == MAGIC;
  54. }
  55.  
  56. HREGEXP AllocateHandle(void)
  57. {
  58.  
  59.     if ( FreeList )
  60.     {
  61.         HREGEXP Handle = (HREGEXP) FreeList;
  62.  
  63.         FreeList = (int)Handles[FreeList];
  64.  
  65.         return Handle;
  66.     }
  67.     else
  68.         return (HREGEXP) 0;
  69. }
  70.  
  71. void FreeHandle(HREGEXP Handle)
  72. {
  73.     Handles[(int) Handle] = (regexp *)FreeList;
  74.     FreeList = (int) Handle;
  75. }
  76.  
  77. // Exported function -----------------------------------------------
  78.  
  79. HREGEXP _export FAR PASCAL RegComp(const char *pattern, int *error)
  80. {
  81.     HREGEXP Handle = AllocateHandle();
  82.  
  83.     if ( Handle )
  84.     {
  85.         Handles[(int) Handle] = regcomp(pattern);
  86.         if ( Handles[(int) Handle] == NULL )
  87.         {
  88.             *error = regerror;
  89.             FreeHandle(Handle);
  90.             return (HREGEXP) 0;
  91.         }
  92.     }
  93.     else
  94.         *error = RE_OUTOFMEMORY;
  95.  
  96.     return Handle;
  97. }
  98.  
  99. int _export FAR PASCAL RegExec(HREGEXP reg, const char *string,
  100.   regmatch *match)
  101. {
  102.     if ( VerifyHandle(reg) )
  103.     {
  104.         regexp *preg = Handles[(int) reg];
  105.  
  106.         regerror = 0;
  107.  
  108.         if ( regexec(preg, string) )
  109.         {
  110.             match->start = preg->startp[0] - (char *)string;
  111.             match->stop  = preg->endp[0] - (char *)string;
  112.  
  113.             return 0;
  114.         }
  115.         else
  116.             return regerror != 0 ? regerror : RE_NOTFOUND;
  117.     }
  118.     else
  119.         return RE_INVALIDPARAMETER;
  120. }
  121.  
  122. #pragma argsused
  123. size_t _export FAR PASCAL RegError(HREGEXP reg, int errcode,
  124.   char *errbuf, size_t errbuf_size)
  125. {
  126.     // "reg" could be used to provide more information about
  127.     // the error.
  128.  
  129.     LoadString(Instance, errcode, errbuf, errbuf_size);
  130.  
  131.     return 0;
  132. }
  133.  
  134. void _export FAR PASCAL RegFree(HREGEXP reg)
  135. {
  136.     if ( VerifyHandle(reg) )
  137.     {
  138.         free(Handles[(int) reg]);
  139.  
  140.         FreeHandle(reg);
  141.     }
  142. }
  143.  
  144. // DLL initialization and shutdown ---------------------------------
  145.  
  146. #pragma argsused
  147. int FAR PASCAL LibMain(HINSTANCE hInstance, WORD wDataSegment,
  148.   WORD wHeapSize, LPSTR lpszCmdLine)
  149. {
  150.     // Initialize the handle table
  151.     InitializeHandles();
  152.  
  153.     Instance = hInstance;
  154.  
  155.     return 1;   // Indicate that the DLL was initialized successfully.
  156. }
  157.  
  158. #pragma argsused
  159. int FAR PASCAL WEP ( int bSystemExit )
  160. {
  161.     return 1;
  162. }
  163.